package com.xphuong.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Typeface;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.xphuong.model.News;
import com.xphuong.newskios.R;
import com.xphuong.newskios.util.Util;

public class NewsListAdapter extends BaseAdapter {

	LayoutInflater inflater;
	private ArrayList<News> mList;
	private Context mContext;

	public static final String LINK = "<html><head><meta name='viewport' content='width=device-width'><title></title></head><body style='margin: 0px;' cz-shortcut-listen='true'><img style='-webkit-user-select: none' src='http://dantri21.vcmedia.vn/zoom/130_100/2togCcWP25hx4bn5VbuB/Image/2014/03/WP_20140311_013-22b8d.jpg'></body></html>";

	public ArrayList<News> getList() {
		return mList;
	}

	public NewsListAdapter(Context ctx, ArrayList<News> lstNews) {
		mContext = ctx;
		inflater = LayoutInflater.from(mContext);

		mList = lstNews;
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;

		if (convertView == null) {
			convertView = (ViewGroup) inflater.inflate(R.layout.news_list_item,
					null);
			holder = new ViewHolder();

			holder.imageWv = ((WebView) convertView.findViewById(R.id.image_wv));
			holder.titleTv = ((TextView) convertView
					.findViewById(R.id.title_tv));
			holder.contentTv = ((TextView) convertView
					.findViewById(R.id.content_tv));
			holder.dateTv = ((TextView) convertView.findViewById(R.id.date_tv));

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		News n = (News) getItem(position);
		Typeface typeTitleFace = Typeface.createFromAsset(mContext.getAssets(),
				"RobotoLight.ttf");
		Typeface typeDateFace = Typeface.createFromAsset(mContext.getAssets(),
				"RobotoLightItalic.ttf");

		holder.imageWv.loadDataWithBaseURL(null, Util.getImage(n.getImage()),
				"text/html", "UTF-8", null);

		holder.titleTv.setTypeface(typeTitleFace);
		holder.contentTv.setTypeface(typeTitleFace);
		holder.dateTv.setTypeface(typeDateFace);

		holder.titleTv.setText(n.getTitle());
		holder.contentTv.setText(Html.fromHtml(n.getDescription()));
		holder.dateTv.setText(n.getPubDate());

		return convertView;
	}

	static class ViewHolder {
		WebView imageWv;
		TextView titleTv;
		TextView contentTv;
		TextView dateTv;
	}

}
